﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentETL;
using FluentETL.Impl;

namespace FluentETL_Examples
{
    public static class Example_Simple
    {
        public static void Run()
        {
            Random r = new Random();
            ISource src = new ObjectSource()
                                        .From(from n in Enumerable.Range(1, 20)
                                              let name = "Person " + n
                                              let birthday = new DateTime(1970, 1, 1).AddDays(r.Next(7000)) // Random date
                                              select new { Name = name, Birthday = birthday });
            IDestination dest = new TextDestination()
                                        .To(Console.Out)
                                        .WithHeader(true)
                                        .WriteAsFixedColumns(new int[] { 15, 15, 15 }, " | ");

            Transform t = new Transform(src, dest)
                                    .Map("Name")
                                    .Map<DateTime, int>("Birthday", "Age", birthday => GetAge(birthday))
                                    .Execute();
        }

        private static int GetAge(DateTime birthday)
        {
            DateTime today = DateTime.Today;
            int years = today.Year - birthday.Year;
            // subtract another year if birthday not yet past
            if (today.Month < birthday.Month || (today.Month == birthday.Month && today.Day < birthday.Day))
                years--;

            return years;
        }
    }
}
